package com.fch.sdlibrary.controller;

import com.fch.sdlibrary.api.BaseResponse;
import com.fch.sdlibrary.entity.Book;
import com.fch.sdlibrary.service.BookService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;


/**
 * @program: sdlibrarySpring
 * @description: 搜索控制类
 * @CreatedBy: fch
 * @create: 2022-05-05 20:27
 **/
@CrossOrigin
@RestController
public class SearchController {
    private BaseResponse baseResponse;
    @Autowired
    private BookService bookService;

    /**
     * @Author: fch
     * @Description: 搜索图书
     * @DateTime: 22:14 7/5/2022
     * @Params: [name, code, pageNum, pageSize]
     * @Return: com.fch.sdlibrary.api.BaseResponse
     **/
    @GetMapping("SearchBook")
    public BaseResponse searchBook(@RequestParam(value = "name", required = false, defaultValue = "") String name,
                                      @RequestParam(value = "code", required = false, defaultValue = "-1") String code,
                                      @RequestParam(value = "pageNum", required = false, defaultValue = "1") Integer pageNum,
                                      @RequestParam(value = "pageSize", required = false, defaultValue = "6") Integer pageSize) {
        baseResponse = new BaseResponse();
        Book book = new Book();
        if (!"".equals(name))
            book.setBookName("%" + name + "%");
        if (!"".equals(code) && !"-1".equals(code))
            book.setBookClassCode(code + "%");
        PageInfo<Book> books = bookService.searchBookByNameAndClazz(book, pageNum, pageSize);
        baseResponse.setCode(200);
        if (books.getList().isEmpty())
            baseResponse.setMsg("查询为空");
        baseResponse.setData(books);
        return baseResponse;
    }

    @GetMapping("SearchBookByIsbn")
    public BaseResponse searchBookByIsbn(@RequestParam String isbn,
                                      @RequestParam(value = "pageNum", required = false, defaultValue = "1") Integer pageNum,
                                      @RequestParam(value = "pageSize", required = false, defaultValue = "6") Integer pageSize) {
        baseResponse = new BaseResponse();
        Book book = new Book();
        book.setBookIsbn(isbn);
        PageInfo<Book> books = bookService.searchBookByIsbn(book, pageNum, pageSize);
        baseResponse.setCode(200);
        if (books.getList().isEmpty())
            baseResponse.setMsg("查询为空");
        baseResponse.setData(books);
        return baseResponse;
    }
}
